package map.coreui.view

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import com.baidu.mapapi.search.poi.PoiDetailResult
import com.baidu.mapapi.search.poi.PoiDetailSearchResult
import com.baidu.mapapi.utils.DistanceUtil
import com.zjw.zy.R
import com.zjw.zy.base.BaseViewGroup
import com.zjw.zy.utils.ResourcesUtils
import map.location.LocationManager
import map.utils.MapManagerFactory

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/10/24 14:08
 */
class PoiDetailView : BaseViewGroup {

    private val title: TextView by lazy { findViewById(R.id.poi_detail_view_title) }
    private val address: TextView by lazy { findViewById(R.id.poi_detail_view_address) }
    private val phone: TextView by lazy { findViewById(R.id.poi_detail_view_phone) }
    private val go: Button by lazy { findViewById(R.id.poi_detail_view_go) }
    private val detailsUrl: Button by lazy { findViewById(R.id.poi_detail_view_details) }
    private val rating: TextView by lazy { findViewById(R.id.poi_detail_view_rating) }
    private val price: TextView by lazy { findViewById(R.id.poi_detail_view_price) }
    private val tag: TextView by lazy { findViewById(R.id.poi_detail_view_tag) }
    private val workTime: TextView by lazy { findViewById(R.id.poi_detail_view_work_time) }
    private val distance: TextView by lazy { findViewById(R.id.poi_detail_view_distance) }

    private var poiDetailSearchResult: PoiDetailResult? = null

    private var mListener: PoiDetailListener? = null

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
    }

    override fun onLayoutId(): Int {
        return R.layout.poi_detail_view
    }

    override fun onBindListener() {
        go.setOnClickListener {
            poiDetailSearchResult?.let { poi ->
                mListener?.onClickGo(
                    poi
                )
            }
        }

        detailsUrl.setOnClickListener {
            poiDetailSearchResult?.let { poi ->
                mListener?.onClickDetails(
                    poi.detailUrl
                )
            }
        }

        phone.setOnClickListener {
            mListener?.onClickPhone(phone.text)
        }

    }

    override fun onBindValue() {
    }

    override fun onFindView(rootView: View) {

    }

    fun bindData(details: PoiDetailResult) {
        poiDetailSearchResult = details
        poiDetailSearchResult?.let {
            title.text = it.name
            address.text = it.address
            phone.text = it.telephone
            rating.text = ResourcesUtils.getString(R.string.poi_detail_rating, it.overallRating)
            price.text = ResourcesUtils.getString(R.string.poi_detail_price, it.price)
            tag.text = it.tag
            workTime.text = ResourcesUtils.getString(R.string.poi_detail_work_time)
            distance.text = ResourcesUtils.getString(
                R.string.poi_detail_work_distance,
                DistanceUtil.getDistance(
                    it.location,
                    MapManagerFactory.get(LocationManager::class.java).locationLatLng()
                )/1000
            )
        }
    }

    fun bindListener(listener: PoiDetailListener) {
        mListener = listener
    }


    interface PoiDetailListener {
        fun onClickGo(poi: PoiDetailResult)
        fun onClickDetails(detailUrl: String)
        fun onClickPhone(text: CharSequence?)
    }
}